package com.btpj.lib_webview

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.net.http.SslError
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.webkit.*
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.launcher.ARouter
import com.btpj.jetpack.utils.StatusBarUtil
import kotlinx.android.synthetic.main.activity_web_view.*

class WebViewActivity : AppCompatActivity() {

    @Autowired
    lateinit var title: String

    @Autowired
    lateinit var url: String

    companion object {

        fun newIntent(context: Context?, title: String, url: String): Intent {
            return Intent(context, WebViewActivity::class.java).apply {
                putExtra("title", title)
                putExtra("url", url)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ARouter.getInstance().inject(this)
//        StatusBarUtil.setImmersionStatus(this)
        setContentView(R.layout.activity_web_view)
        initActionBar()
        initWebView()
        iv_collect.setOnClickListener {  }
    }


    private fun initActionBar() {
        tv_title.text = title
        iv_back.setOnClickListener {
            finish()
        }
    }

    @SuppressLint("JavascriptInterface", "SetJavaScriptEnabled")
    private fun initWebView() {
        val settings = webView.settings
        settings.allowContentAccess = true
        settings.domStorageEnabled = true
        settings.allowFileAccess = true
        settings.javaScriptEnabled = true

        webView.addJavascriptInterface(this, "wan")

        webView.webChromeClient = object : WebChromeClient() {

        }

        webView.webViewClient = object : WebViewClient() {
            override fun onReceivedSslError(
                view: WebView?,
                handler: SslErrorHandler?,
                error: SslError?
            ) {
                handler?.proceed()
            }

            override fun shouldOverrideUrlLoading(
                view: WebView?,
                request: WebResourceRequest?
            ): Boolean {
                return super.shouldOverrideUrlLoading(view, request)
            }
        }

        webView.loadUrl(url)
    }
}